import java.util.*;

public class LeetCode {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
    }
    LinkedList<List<Integer>> List1 = new LinkedList<>();
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        if(root == null){
            return List1;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int len = 0;
        while(!queue.isEmpty()){
            len = queue.size();
            List<Integer> list = new ArrayList<>();
            while(len > 0){
                TreeNode cur = queue.poll();
                list.add(cur.val);
                //System.out.print(cur.val+" ");
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                len--;
            }
            List1.addFirst(list);
        }
        return List1;
    }
}
